package a00_ImageGame.Test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @Author quan
 * @Description 鼠标监听机制
 * @Date 2023/3/7 10:59
 */
//--------方法实现接口
public class MyMouseListener extends JFrame implements MouseListener {
    public MyMouseListener() {//-----类
        JFrame jFrame = new JFrame();
        jFrame.setSize(400,600);
        jFrame.setTitle("事件演示");
        jFrame.setAlwaysOnTop(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setLayout(null);

        //创建一个按钮
        JButton jButton = new JButton("点我~");
        jButton.setBounds(0,10,100,100);
        //----------绑定事件到按钮中
        jButton.addMouseListener(this);

        //将按钮添加到桌面的容器中
        jFrame.getContentPane().add(jButton);
        jFrame.setVisible(true);

    }

    //----------重写MouseListenser接口中的所有方法
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("111");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("222");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("333");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("444");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("555");
    }

    //测试
    public static void main(String[] args) {
        new MyMouseListener();
    }
}


